const BASEURL = 'http://jacklv.cn'
export default {
    post(url, data, method) {
        if (method) {
            return fetch(BASEURL + url + '?id=' + data.id, {
                    method: method,
                    body: JSON.stringify(data),
                    headers: {
                        "Content-Type": "application/json",
                        "token": localStorage.token
                    }
                })
                .then(response => {
                    switch (response.status) {
                        case 200:
                            return response.json()
                        case 401:
                            window.location.hash = '#/login'
                            break
                    }
                })
                .then(res => {
                    // console.log(res);
                    return res
                })
                .catch(err => {
                    return err
                })
        } else {
            return fetch(BASEURL + url, {
                    method: "POST",
                    body: JSON.stringify(data),
                    headers: {
                        "Content-Type": "application/json",
                        "token": localStorage.token
                    }
                })
                .then(response => {
                    switch (response.status) {
                        case 200:
                            return response.json()
                        case 401:
                            window.location.hash = '#/login'
                            break
                    }

                })
                .then(res => {
                    // console.log(res);
                    return res
                })
                .catch(err => {
                    return err
                    console.log(err);
                })
        }

    },
    get(url, data) {
        return fetch(BASEURL + url, {
                method: "GET",
                body: JSON.stringify(data),
                headers: {
                    "Content-Type": "application/json",
                    "token": localStorage.token
                }
            })
            .then(response => {
                switch (response.status) {
                    case 200:
                        return response.json()
                    case 401:
                        window.location.hash = '#/login'
                        break
                }
            })
            .then(res => {
                console.log(res);
            })
            .catch(err => {
                console.log(err);
            })
    }
}